//
//  CNStatusCell.swift
//  Ccweibo
//
//  Created by 陈锦聪 on 16/3/26.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit
import SnapKit

protocol CNStatusCellDelegate: NSObjectProtocol {
    func cellDidClickRetweetButton(cell: CNStatusCell)
    func cellDidClickCommentButton(cell: CNStatusCell)
}

class CNStatusCell: UITableViewCell {
    
    weak var delegate: CNStatusCellDelegate?
    
    private var buttonViewTopConstraint: Constraint?
    
    var status: CNStatus? {
        didSet {
            originalView.status = status
            buttonViewTopConstraint?.uninstall()
            if let retweetStatus = status?.retweeted_status {
                retweetView.hidden = false
                retweetView.retweetStatus = retweetStatus
                buttonView.snp_makeConstraints(closure: { (make) -> Void in
                    buttonViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {
                retweetView.hidden = true
                buttonView.snp_makeConstraints(closure: { (make) -> Void in
                    buttonViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
    }
    
    private func prepareUI() {
        
        backgroundColor = UIColor.init(white: 237 / 255.0, alpha: 1)
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(buttonView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(defaultMargin)
            make.leading.trailing.equalTo(contentView)
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        buttonView.snp_makeConstraints { (make) -> Void in
            buttonViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(buttonView)
        }
        // cell的优化
        // 异步绘制
        self.layer.drawsAsynchronously = true
        // 栅格化
        self.layer.shouldRasterize = true
        // 光栅比例等于屏幕的缩放比例(影响清晰度)
        self.layer.rasterizationScale = UIScreen.mainScreen().scale
    }

    private lazy var originalView: CNStatusOriginalView = CNStatusOriginalView()
    
    private lazy var retweetView: CNStatusRetweetView = CNStatusRetweetView()
    
    private lazy var buttonView: CNStatusButtonView = {
        let view = CNStatusButtonView()
        view.delegate = self
        return view
    }()
}

extension CNStatusCell: CNStatusButtonViewDelegate {
    func didClickRetweetButton() {
        delegate?.cellDidClickRetweetButton(self)
    }
    func didClickCommentButton() {
        delegate?.cellDidClickCommentButton(self)

    }
}
